package dasom.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import dasom.model.MemberVO;
import dasom.util.OracleJDBC;

/*회원가입,로그인,회원정보 출력 담당 클래스*/
public class MemberDAO {
	
	protected static String joinMember="insert into member(mno,userid,passwd,name,email) values(mbseq.nextval,?,?,?,?)";
	protected static String loginMember="select count(mno) from member where userid=? and passwd=?";
	protected static String showMember="select * from member where userid=?";
	
	//회원가입 처리 메서드:
	//회원가입 성공여부-result변수
	//1)가입ok:result에 true 저장
	//2)가입no:result에 false 저장(초기값)
	public boolean joinMember(MemberVO mvo){
		Connection conn=null;
		PreparedStatement ps = null;
		boolean result=false;  //2)
		
		try{
			conn = OracleJDBC.OpenConn();
			ps = conn.prepareStatement(joinMember);
				ps.setString(1, mvo.getUserid());
				ps.setString(2, mvo.getPasswd());				
				ps.setString(3, mvo.getName());
				ps.setString(4, mvo.getEmail());
			if(ps.executeUpdate()>0)result=true;  //1)	
				
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			OracleJDBC.closeConn(conn, ps, null);
		}
		return result;
	}
		
	//로그인 처리 메서드:
	//아이디,비번확인 여부-result변수
	//1)아이디,비번확인ok:result에 true저장
	//2)아이디,비번확인no:result에 false저장(초기값)
	
	public boolean loginMember(MemberVO mvo){
		Connection conn=null;
		PreparedStatement ps= null;
		ResultSet rs=null;
		boolean result=false;
		
		
		try{
			conn=OracleJDBC.OpenConn();
			ps=conn.prepareStatement(loginMember);
				ps.setString(1, mvo.getUserid());
				ps.setString(2, mvo.getPasswd());
			rs=ps.executeQuery();
			
			if(rs.next()) {
				if(rs.getInt(1)>0)result=true;
				//select count(mno)from member
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			OracleJDBC.closeConn(conn, ps, rs);
		}                                                         
		return result;
	}
	

//회원정보 조회 메서드
//세션변수로 선언된 userid를 이용해서 데이터베이스에서 로그인한 회원의 상세정보를 읽어서 MemberVO 객체에 저장
	public MemberVO showMember(String uid){
		Connection conn=null;
		PreparedStatement ps= null;
		ResultSet rs=null;
		MemberVO mvo=null;
		
		try{
			conn=OracleJDBC.OpenConn();
			ps=conn.prepareStatement(showMember);
				ps.setString(1, uid);
			rs=ps.executeQuery();
			if(rs.next()){
				mvo=new MemberVO();
				mvo.setName(rs.getString(4));
				mvo.setEmail(rs.getString(5));
				mvo.setRegdate(rs.getString(6));
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			OracleJDBC.closeConn(conn, ps, rs);
		}
		return mvo; 
	}

}